package 第十章_多线程与并发;

import static java.util.stream.IntStream.range;

public class U10_L8_线程小案例 {
    public static void main(String [] args){
        Food food=new Food();
        Producters p=new Producters(food);
        Customers c=new Customers(food);
        Thread t1=new Thread(p);
        Thread t2=new Thread(c);
        t1.setName(" t1(制造者) ");
        t2.setName(" t2(消费者) ");
        t1.start();
        t2.start();


    }
}

class Food{
    private String name;
    private String desc;
    private boolean flag=true;
    //生产产品的 设置
    public synchronized void set(String name,String desc){
        if(!flag){
            try {
                this.wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        this.setName(name);
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        this.setDesc(desc);
        flag=false;
        this.notify();
    }
    public synchronized void get(){
        if(flag){
            try {
                this.wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println(Thread.currentThread().getName() + "取得食物"+this.getName()+"->"+this.getDesc());
        flag=true;
        this.notify();
    }
    public Food() {
    }

    public Food(String name, String desc) {
        this.name = name;
        this.desc = desc;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    @Override
    public String toString() {
        return "food{" +
                "name='" + name + '\'' +
                ", desc='" + desc + '\'' +
                '}';
    }
}

class Producters implements Runnable{
    private Food food;
    public Producters(Food food){
        this.food=food;
    }

    @Override
    public void run() {
        int count=0;
        for(int i=0;i<20;i++){
            if(i%2==0){
                count++;
                food.set("酸菜鱼","酸甜可口-酸");
                System.out.println(Thread.currentThread().getName()+"制作食物: "+food.getName()+" ,次数:"+count+" ");
            } else if (i%2==1) {
                count++;
                System.out.println(Thread.currentThread().getName()+"制作食物: "+food.getName()+" ,次数:"+count+" ");
                food.set("咖喱鸡","可口美味-咖");


            }else{
                count++;
                System.out.println(Thread.currentThread().getName()+"制作食物: "+food.getName()+" ,次数:"+count+" ");
                food.set("水煮牛肉","滑嫩丝滑-水");

            }
        }
    }
}

class Customers implements Runnable{
    private Food food;

    public Customers(Food food) {
        this.food = food;
    }

    @Override
    public void run() {
        range(0, 20).forEach(this::accept);
    }

    private void accept(int i) {
        food.get();

    }
}